import Api from "@/api";

let colorTable = {};
let fieldLayer = {}; //场数据图层

const flowParticle = {
  init: () => {
    const floor = 0.1;
    const ceil = 1.6841634341693739;
    const dx = (ceil - floor) / 8;

    const dataBounds = Cesium.Rectangle.fromDegrees(
      114.1499221,
      30.38574594,
      114.5742016,
      30.70307222
    );
    colorTable = new Cesium.ColorTable();

    colorTable.insert(floor, new Cesium.Color.fromCssColorString("#6AF700"));
    colorTable.insert(
      floor + dx,
      new Cesium.Color.fromCssColorString("#B1FD02")
    );
    colorTable.insert(
      floor + dx * 2,
      new Cesium.Color.fromCssColorString("#FCF601")
    );
    colorTable.insert(
      floor + dx * 3,
      new Cesium.Color.fromCssColorString("#FCF601")
    );
    colorTable.insert(
      floor + dx * 4,
      new Cesium.Color.fromCssColorString("#FDBC00")
    );
    colorTable.insert(
      floor + dx * 5,
      new Cesium.Color.fromCssColorString("#F99A00")
    );
    colorTable.insert(
      floor + dx * 6,
      new Cesium.Color.fromCssColorString("#FF7005")
    );
    colorTable.insert(
      floor + dx * 7,
      new Cesium.Color.fromCssColorString("#FF4B04")
    );
    fieldLayer = new Cesium.FieldLayer3D(window.viewer.scene.context);
    fieldLayer.particleVelocityFieldEffect._setDataBounds(dataBounds);
    fieldLayer.particleVelocityFieldEffect.colorTable = colorTable;
    //初始化效果
    fieldLayer.particleVelocityFieldEffect.velocityScale = 0.1;
    fieldLayer.particleVelocityFieldEffect.particleSize = 4;
    fieldLayer.particleVelocityFieldEffect.paricleCountPerDegree = 500;
    //添加场图层
    window.viewer.scene.primitives.add(fieldLayer);

    let particleWindField = [];
    let particleWindInverseField = [];
    //加载数据
    Api.getWaterFieldData2().then((data) => {
      let p = 0;
      for (let j = 0; j < data.ny; j++) {
        particleWindField[j] = [];
        particleWindInverseField[j] = [];
        for (let i = 0; i < data.nx; i++, p++) {
          particleWindField[j][i] = data.data[p];
          particleWindInverseField[j][i] = [-data.data[p][0], -data.data[p][1]];
        }
      }
      fieldLayer.fieldData = particleWindField;
      // fieldLayer.visible = false;
      console.log("流场图层:初始化完成");
    });
  },

  add: () => {
    fieldLayer.visible = true;
  },
  remove: () => {
    fieldLayer.visible = false;
  },

  changeSize: (value) => {
    fieldLayer.particleVelocityFieldEffect.particleSize = value;
  },
  changeLifeRange: (value) => {
    const lifeRange = [value * 1000.0, value * 1000.0 + 5000.0];
    fieldLayer.particleVelocityFieldEffect.particleLifeRange = lifeRange;
  },
  changeCountPerDegree: (value) => {
    fieldLayer.particleVelocityFieldEffect.paricleCountPerDegree = value;
  },
  changeVelocityScale: (value) => {
    fieldLayer.particleVelocityFieldEffect.velocityScale = value * 100.0;
  },
};

export default flowParticle;
